package com.tevintao.marketingsystem.task;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.HookSensor;
import com.tevintao.marketingsystem.xposted.WxTaskManger;

import java.util.Random;


/**
 * 通过搜索添加好友，接受好友
 */
public class ShakeShakeProcess extends SetTaskExtra {
    private static ShakeShakeProcess ourInstance = new ShakeShakeProcess();

    public static ShakeShakeProcess getInstance() {
        return ourInstance;
    }

    private ShakeShakeProcess() {
    }


    private  int searchFriendNum=0;
    private  int wantFriendNum=5;//想要的朋友个数(达到后不再添加)
    private  String lastfriend="";//上一个摇到的朋友名字
    private String sexfriend="";//摇到的朋友性别
    private String stringsexfriend="";//摇到的朋友  性别相关的字符串，可以从中提取出朋友的性别
    private  int sexFriendindex=1;//0代表不区分性别，1代表男生，2代表女生
    private  boolean ifItsFirst=false;//是否是从摇一摇界面外部入口  初次进入摇一摇
    private  boolean ifSearchFriend=false;//是否摇到朋友
    private Boolean ifCall = false;//是否打过招呼了。是的话，返回附近人列表，没有的话就 接着打招呼
    private int mSelectPosition = -1;
    private Random mRandom = new Random();

    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {
        v.getContentDescription();
        LogManager.getInstance().logInfo("ShakeShakeProcess processView contextName = " + contextName + " v = " + v.toString());
        //WxUiViewConfig.printViewGroup((ViewGroup) v);
        if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {
            boolean inDiscover = WxUiViewConfig.inTab(v, WxUiViewConfig.TAB_DISCOVER);
            LogManager.getInstance().logInfo("intab = " + inDiscover);
            if (inDiscover) {
                View shakeView = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v,16908310, "摇一摇");
                LogManager.getInstance().logInfo("shakeView = " + shakeView);
                if (null != shakeView) {
                    ViewOperation.clickView(shakeView);
                    ifItsFirst=true;
                }
            }
            // mSelectPosition = -1;
        }else if("com.tencent.mm.plugin.shake.ui.ShakeSayHiListUI".equals(contextName)){
            LogManager.getInstance().logInfo("当前位于摇一摇给我打招呼的人 的列表");

            View lookMoreView = v.findViewById(2131627359);
            if (null != lookMoreView) {
                ViewOperation.clickView(lookMoreView);
                LogManager.getInstance().logInfo("点击了查看更多");

            }
            View listView = v.findViewById(2131626199);
            LogManager.getInstance().logInfo("listView = " + listView);
            if (null != listView && listView instanceof ListView) {
                ListView list = (ListView) listView;
                if (mSelectPosition < 0) {
                    mSelectPosition = 0;
                }else if (mSelectPosition < list.getAdapter().getCount() + list.getHeaderViewsCount()){
                    //（mSelectPosition的取值范围为0到list.getAdapter().getCount()-2+list.getHeaderViewsCount） 即是从第一个好友到最后一个好友
                    mSelectPosition++;
                }else {
                    //以下为清空列表操作  暂时注释掉

                    //mSelectPosition=-1;
                    //clearView = getViewByIdAndContent((ViewGroup) v, 2131624138, "清空");
                    //break;
                }
                int first = list.getFirstVisiblePosition();
                int last = list.getLastVisiblePosition();
                LogManager.getInstance().logInfo("mSelectPosition < 0 mSelectPosition = " + mSelectPosition + " first = " + first + " last =  " + last + " headerCount = " + list.getHeaderViewsCount() + list.getAdapter().getCount() + "  " + list.getCount() + " " + list.getFooterViewsCount());
                if (mSelectPosition < first || mSelectPosition > last) {
                    ViewOperation.moveListPosition(list, mSelectPosition);
                    LogManager.getInstance().logInfo("不到位");
                    WxUiViewConfig.printViewGroup((ViewGroup) v);
                } else {
                    //mSelectPosition = -1;
                    LogManager.getInstance().logInfo("移动到位");

                    View currentvisibleView = list.getChildAt(mSelectPosition);
                    WxUiViewConfig.printViewGroup((ViewGroup) v);


                    if (null != currentvisibleView) {
                        ViewOperation.clickView(currentvisibleView);
                        searchFriendNum++;
                    }
                }
            }

        }
        else if("com.tencent.mm.plugin.profile.ui.ContactInfoUI".equals(contextName)){
            WxUiViewConfig.printViewGroup((ViewGroup) v);
            if (ifCall == false) {
                LogManager.getInstance().logInfo("点击了摇到的人    或者点击了给我打招呼的人  进入了打招呼界面");
                //首先找到打招呼按钮
                View buttonViewCall = v.findViewById(2131625116);
                View buttonViewThrough = v.findViewById(2131625111);
                if (null != buttonViewCall && buttonViewCall instanceof Button) {
                    ViewOperation.clickView(buttonViewCall);
                }
                if (null != buttonViewThrough && buttonViewThrough instanceof Button){
                    ViewOperation.clickView(buttonViewThrough);
                }
                ifCall = true;
            } else {
                LogManager.getInstance().logInfo("发送了加人请求后    返回了打招呼界面 ( 如果对方没有设置好友验证 也返回到同一个 activty (但是界面内容稍有不同) 并且直接成为了好友)");
                //找到返回按钮   按下后会返回附近人好友列表
                View buttonViewBack = v.findViewById(2131624160);
                if (null != buttonViewBack) {
                    ViewOperation.clickView(buttonViewBack);
                }
                ifCall = false;
                LogManager.getInstance().logInfo("按下了返回按钮");
            }

        } else if ("com.tencent.mm.ui.contact.SayHiEditUI".equals(contextName)) {
            LogManager.getInstance().logInfo("打招呼内容输入界面");
            WxUiViewConfig.printViewGroup((ViewGroup) v);

            View editViewShuRu = v.findViewById(2131627356);
            if (editViewShuRu instanceof EditText) {
                EditText textView = (EditText) editViewShuRu;
                if ("".equals(textView.getText().toString())) {
                    //TextView textView = (TextView) buttonViewFaSong;
                    LogManager.getInstance().logInfo("等待自动输入评论内容 ");
                    String[] str = new String[]{
                            "嗨，你好", "你好啊", "嗨，好久不见了啊", "最近生活不错啊", "毕业了吧", "现在在哪里工作呢", "你怎么也来这边了", "相逢何必曾相识", "亲，忙什么呢", "四海之内皆朋友"
                    };
                    ViewOperation.setText(textView, str[mRandom.nextInt(10)]);
                } else {
                    View enterView = v.findViewById(2131624139);

                    TextView enterbutton = (TextView) enterView;
                    if ("发送".equals(enterbutton.getText().toString())) {
                        ViewOperation.clickView(enterbutton);
                        LogManager.getInstance().logInfo("自动输入打招呼内容之后，按下了发送按钮 ");
                        LogManager.getInstance().logInfo("在摇一摇中   第"+searchFriendNum+"次打招呼添加好友");

                    }
                }
            }

        } else
        if (WxUiViewConfig.VIEW_SHAKE_SHAKE.equals(contextName)) {
                WxUiViewConfig.printViewGroup((ViewGroup) v);
                View progressView = WxUiViewConfig.getViewByClassName((ViewGroup) v,"ProgressBar");
                View noresultView = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v,2131627528, "无结果，请重新再试一次");
                View friendView = v.findViewById(2131627533);
                View searchView = v.findViewById(2131627526);
                View searchfriendView = v.findViewById(2131627531);
                View helloView = v.findViewById(2131626038);//别人给我打招呼
                View helloNumView = v.findViewById(2131626040);//招呼个数标签
                TextView textView = (TextView) friendView;

                if(searchView!=null&&searchView.getVisibility()==View.VISIBLE){
                    LogManager.getInstance().logInfo("正在搜索");
                    //v.getContext().sendBroadcast(new Intent(HookSensor.ACTION_START_SHAKE));
                }else if(searchFriendNum<=wantFriendNum){
                        TextView hellonumView = (TextView) helloNumView;
                        if (!"你收到%d个打招呼消息".equals(hellonumView.getText().toString())) {
                            LogManager.getInstance().logInfo("有人在摇一摇页面给我打招呼  点击进入给我打招呼的人 的列表");
                            ViewOperation.clickView(helloView);
                        }else
                        if (!"".equals(textView.getText().toString())&&!lastfriend.equals(textView.getText().toString())){
                            lastfriend=textView.getText().toString();
                            if (null != searchfriendView)
                                stringsexfriend=(String)searchfriendView.getContentDescription();
                            sexfriend = stringsexfriend.substring(stringsexfriend.length()-1,stringsexfriend.length());
                            LogManager.getInstance().logInfo("~~~~~~~~~~~~~~~~~~"+sexfriend);
                            switch (sexFriendindex) {
                                case 0:
                                    ViewOperation.clickView(searchfriendView);
                                    searchFriendNum++;
                                    LogManager.getInstance().logInfo("选择 不区分性别 的 添加");
                                    break;
                                case 1:
                                    if("男".equals(sexfriend)) {
                                        ViewOperation.clickView(searchfriendView);
                                        searchFriendNum++;
                                        LogManager.getInstance().logInfo("选择了只添加男生");
                                    }
                                    break;
                                case 2:
                                    if("女".equals(sexfriend)) {
                                        ViewOperation.clickView(searchfriendView);
                                        searchFriendNum++;
                                        LogManager.getInstance().logInfo("选择了只添加女生");
                                    }
                                    break;
                            }
                    }else{
                        LogManager.getInstance().logInfo("没有摇到朋友    或者初次进入   或者摇到已经招呼   或者摇到不符合性别标准    开始摇一摇");
                        beginShake(v);
                    }
                }else
                    taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "已经通过摇一摇添加了所需要的"+wantFriendNum+"个朋友     结束任务");

        }
        return false;
    }

    private void beginShake(View v){
        if (WxTaskProcess.DEBUG_MODEL)
            v.getContext().sendBroadcast(new Intent(HookSensor.ACTION_START_SHAKE));
        else {
            HookSensor.getInstance().startShake();
        }
    }
}
